Operator | Description | Example |
---|---|---|
~ | Inverts the bits in a number so that all the 0 bits become 1 bits and vice versa. | ~4 => –5 |
- | Negates the original value so that positive becomes negative and vice versa. | –(–4) => 4 –4 => –4 |
+ | Is provided purely for the sake of completeness. This operator returns the same value that you provide as input. | +4 => 4 |
Operator | Description | Example |
---|---|---|
+ | Adds two values together | 5 + 2 => 7 |
- | Subtracts the right operand from the left operand | 5 – 2 => 3 |
* | Multiplies the right operand by the left operand | 5 * 2 => 10 |
/ | Divides the left operand by the right operand | 5 / 2 => 2.5 |
% | Divides the left operand by the right operand and returns the remainder | 5 % 2 => 1 |
** | Calculates the exponential value of the right operand by the left operand | 5 ** 2 => 25 |
// | Performs integer division, in which the left operand is divided by the right operand and only the whole number is returned (also called floor division) | 5 // 2 => 2 |
The relational operators compare one value to another and tell you when the relationship you’ve provided is true.
Operator | Description | Example |
---|---|---|
== | Determines whether two values are equal. | 1 == 2 is False |
!= | Determines whether two values are not equal. | 1 != 2 is True |
> | Verifies that the left operand value is greater than the right operand value. | 1 > 2 is False |
< | Verifies that the left operand value is less than the right operand value. | 1 < 2 is True |
>= | Verifies that the left operand value is greater than or equal to the right operand value | 1 >= 2 is False |
<= | Verifies that the left operand value is less than or equal to the right operand value. | 1 <= 2 is True |
The logical operators combine the true or false value of variables or expressions so that you can determine their resultant truth value.
Operator | Description | Example |
---|---|---|
and | Determines whether both operands are true. | True and True is True True and False is False False and True is False False and False is False |
or | Determines when one of two operands is true. | True or True is True True or False is True False or True is True False or False is False |
not | Negates the truth value of a single operand. A true value becomes false and a false value becomes true. | not True is False not False is True |
The assignment operators place data within a variable. Python offers a number of other interesting assignment operators that you can use. These other assignment operators can perform mathematical tasks during the assignment process, which makes it possible to combine assignment with a math operation.
Operator | Description | Example |
---|---|---|
= | Assigns the value found in the right operand to the left operand. | A = 2 results in A containing 2 |
+= | Adds the value found in the right operand to the value found in the left operand and places the result in the left operand. | a = 5 a +=2 results in a containing 7 |
-= | Subtracts the value found in the right operand from the value found in the left operand and places the result in the left operand. | a = 5 a -=2 results in a containing 3 |
*= | Multiplies the value found in the right operand by the value found in the left operand and places the result in the left operand. | a = 5 a *=2 results in a containing 10 |
/= | Divides the value found in the left operand by the value found in the right operand and places the result in the left operand. | a = 5 a /=2 results in a containing 2.5 |
%= | Divides the value found in the left operand by the value found in the right operand and places the remainder in the left operand. | a = 5 a %=2 results in a containing 2 |
**= | Determines the exponential value found in the left operand when raised to the power of the value found in the right operand and places the result in the left operand. | a = 5 a **=2 results in a containing 25 |
//= | Divides the value found in the left operand by the value found in the right operand and places the integer (whole number) result in the left operand. | a = 5 a //=2 results in a containing 2 |
A bitwise operator would interact with each bit within the number in a specific way. When working with a logical bitwise operator, a value of 0 counts as false and a value of 1 counts as true.
Operator | Description | Example |
---|---|---|
& (and) | Determines whether both individual bits within two operators are true and sets the resulting bit to true when they are. | 0b1100 & 0b0110 = 0b0100 |
|(or) | Determines whether either of the individual bits within two operators is true and sets the resulting bit to true when one of them is. | 0b1100 | 0b0110 = 0b1110 |
^(Exclusive or) | Determines whether just one of the individual bits within two operators is true and sets the resulting bit to true when one is. When both bits are true or both bits are false, the result is false. | 0b1100 ^ 0b0110 = 0b1010 |
~(One complement) | Calculates the one's complement value of a number. | ~0b1100 = –0b1101 ~0b0110 = –0b0111 |
<<(Left shift) | Shifts the bits in the left operand left by the value of the right operand. All new bits are set to 0 and all bits that flow off the end are lost. | 0b00110011 << 2 break = 0b11001100 |
>>(Right shift) | Shifts the bits in the left operand right by the value of the right operand. All new bits are set to 0 and all bits that flow off the end are lost. | 0b00110011 >> 2 = 0b00001100 |
The membership operators detect the appearance of a value within a list or sequence and then output the truth value of that appearance.
Operator | Description | Example |
---|---|---|
in | Determines whether the value in the left operand appears in the sequence found in the right operand. | "CCIT" in "Welcome to CCIT" =True |
not in | Determines whether the value in the left operand is missing from the sequence found in the right operand. | "CCIT" not in "Welcome to CCIT" =False |
When you create simple statements that contain just one operator, the order of determining the output of that operator is also simple. However, when you start working with multiple operators, it becomes necessary to determine which operator to evaluate first.
Operator | Description |
---|---|
() | You use parentheses to group expressions and to override the default precedence so that you can force an operation of lower precedence (such as addition) to take precedence over an operation of higher precedence (such as multiplication). |
** | Exponentiation raises the value of the left operand to the power of the right operand. |
~ + - | Unary operators interact with a single variable or expression. |
* / % // | Multiply, divide, modulo, and floor division. |
+ - | Addition and subtraction. |
>> << | Right and left bitwise shift. |
& | Bitwise AND |
^ | | Bitwise exclusive OR and standard OR |
<= < > >= | Comparison operators. |
== != | Equality operators. |
= %= /= //= -= += *= **= | Assignment operators. |
in not in | Membership operators. |
not or and | Logical operators |